package com.yitiao.simple.tcp;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.SneakyThrows;

public class NettyClient {
    @SneakyThrows
    public static void main(String[] args) {

        //客户端需要一个事件循环组
        EventLoopGroup group = new NioEventLoopGroup();

        Bootstrap client = new Bootstrap();
        client.group(group)
                .channel(NioSocketChannel.class) // 设置客户端通道的实现类(反射)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new NettyClientHandler()); //加入自己的处理器
                    }
                });
        ChannelFuture channelFuture = client.connect("localhost", 9100).sync();

        //给关闭通道进行监听
        channelFuture.channel().closeFuture().sync();

        group.shutdownGracefully();

    }
}
